package com.ctgu.chatapp.view.fragment.MainFragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.ctgu.chatapp.databinding.FriendGroupBinding
import com.ctgu.chatapp.model.Database.DataBaseManager
import com.ctgu.chatapp.model.Retrofit.IMSServiceImpl
import com.ctgu.chatapp.utils.Utils
import com.ctgu.chatapp.view.fragment.MainFragment.viewPager.FriendListByGroup
import com.ctgu.chatapp.view.fragment.MainFragment.viewPager.FriendListByName
import com.ctgu.chatapp.view.fragment.MainFragment.viewPager.GroupList
import com.ctgu.chatapp.viewModel.MainViewModel
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class FriendAGroup : Fragment() {

    lateinit var binding: FriendGroupBinding

    val viewModel by lazy { ViewModelProvider(requireActivity())[MainViewModel::class.java] }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FriendGroupBinding.inflate(inflater, container, false)
        binding.viewModel = viewModel
        binding.lifecycleOwner = requireActivity()
        initViewPager()
        initListener()
        return binding.root
    }

    private fun initViewPager() {
        binding.viewPager.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount() = 3
            override fun createFragment(position: Int) =
                when (position) {
                    0 -> FriendListByName()
                    1 -> FriendListByGroup()
                    else -> GroupList()
                }
        }
        val arrayTitle = arrayListOf("好友", "分组", "群聊")
        binding.tablelayout.addTab(binding.tablelayout.newTab().setText(arrayTitle[0]))
        binding.tablelayout.addTab(binding.tablelayout.newTab().setText(arrayTitle[1]))
        binding.tablelayout.addTab(binding.tablelayout.newTab().setText(arrayTitle[2]))
        TabLayoutMediator(binding.tablelayout, binding.viewPager, true, false) { tab, position ->
            tab.text = arrayTitle[position]
        }.attach()
    }

    private fun initListener() {
        binding.swipe.setOnRefreshListener {
            lifecycleScope.launch(Main) {
                withContext(IO) {
                    if (Utils.FriendList.isNotEmpty()) {
                        val friendInformation = async {
                            IMSServiceImpl.imsServiceImpl.service.getFriendListInformation(Utils.FriendList)
                        }
                        for (information in friendInformation.await()) {
                            DataBaseManager.getInstance(requireContext()).getInformationDao()
                                .addInformation(information)
                        }
                    }
                }
                binding.swipe.isRefreshing = false
            }
        }
    }
}